//==================================================================
// MTO.H
//
// Revision history
//=================================
//          20030110    UN20 Pete Chao
//                      Initial Release
//
// Copyright (c) 2003 Winbond Electronics Corp. All rights reserved.
//==================================================================
#ifndef __MTO_H__
#define __MTO_H__

// LA20040210_DTO kevin
//#include "platform.h"

//#define MTO_DEFAULT_TH_FIXANT           3
#define MTO_DEFAULT_TH_CNT              5
#define MTO_DEFAULT_TH_SQ3              112  //OLD IS 13 reference JohnXu
#define MTO_DEFAULT_TH_IDLE_SLOT        15
#define MTO_DEFAULT_TH_PR_INTERF        30
#define MTO_DEFAULT_TMR_AGING           25  // unit: slot time  10 reference JohnXu
#define MTO_DEFAULT_TMR_PERIODIC        5   // unit: slot time

#define MTO_ANTENNA_DIVERSITY_OFF       0
#define MTO_ANTENNA_DIVERSITY_ON        1

// LA20040210_DTO kevin
//#define MTO_PREAMBLE_LONG               0
//#define MTO_PREAMBLE_SHORT              1
#define MTO_PREAMBLE_LONG               WLAN_PREAMBLE_TYPE_LONG
#define MTO_PREAMBLE_SHORT              WLAN_PREAMBLE_TYPE_SHORT

typedef enum {
    TOGGLE_STATE_IDLE             = 0,
    TOGGLE_STATE_WAIT0            = 1,
    TOGGLE_STATE_WAIT1            = 2,
    TOGGLE_STATE_MAKEDESISION     = 3,
	TOGGLE_STATE_BKOFF            = 4
} TOGGLE_STATE;

typedef enum {
    RATE_CHGSTATE_IDLE         = 0,
    RATE_CHGSTATE_CALCULATE    = 1,
    RATE_CHGSTATE_BACKOFF	   = 2
} TX_RATE_REDUCTION_STATE;
//============================================================================
// struct _MTOParameters --
//
//   Defines the parameters used in the MAC Throughput Optimization algorithm
//============================================================================
typedef struct _MTO_PARAMETERS
{
    UCHAR      Th_Fixant;
    UCHAR      Th_Cnt;
    UCHAR      Th_SQ3;
    UCHAR      Th_IdleSlot;

	USHORT     Tmr_Aging;
    UCHAR      Th_PrInterf;
    UCHAR      Tmr_Periodic;

	//---------        wkchen added      -------------
	ULONG		TxFlowCount;	//to judge what kind the tx flow(sparse or busy) is
	//------------------------------------------------

	//--------- DTO threshold parameters -------------
	USHORT		DTO_PeriodicCheckCycle;
	USHORT		DTO_RssiThForAntDiv;

	USHORT		DTO_TxCountThForCalcNewRate;
	USHORT		DTO_TxRateIncTh;

	USHORT		DTO_TxRateDecTh;
	USHORT		DTO_TxRateEqTh;

	USHORT		DTO_TxRateBackOff;
	USHORT		DTO_TxRetryRateReduce;

	USHORT		DTO_TxPowerIndex;	//0 ~ 31
	USHORT		reserved_1;
	//------------------------------------------------

    UCHAR      PowerChangeEnable;
    UCHAR      AntDiversityEnable;
    UCHAR      Ant_mac;
    UCHAR      Ant_div;

    UCHAR      CCA_Mode;
    UCHAR      CCA_Mode_Setup;
    UCHAR      Preamble_Type;
    UCHAR      PreambleChangeEnable;

    UCHAR      DataRateLevel;
    UCHAR      DataRateChangeEnable;
    UCHAR      FragThresholdLevel;
    UCHAR      FragThresholdChangeEnable;

    USHORT     RTSThreshold;
    USHORT     RTSThreshold_Setup;

    ULONG     AvgIdleSlot;
    ULONG     Pr_Interf;
    ULONG     AvgGapBtwnInterf;

    UCHAR	   RTSChangeEnable;
    UCHAR      Ant_sel;
    UCHAR      aging_timeout;
	UCHAR	   reserved_2;

    ULONG     Cnt_Ant[2];
    ULONG     SQ_Ant[2];

// 20040510 remove from globe vairable
    ULONG                     TmrCnt;
    ULONG                     BackoffTmr;
    TOGGLE_STATE            ToggleState;
    TX_RATE_REDUCTION_STATE TxRateReductionState;

    UCHAR                      Last_Rate;
    UCHAR                      Co_efficent;
	UCHAR		FallbackRateLevel;
	UCHAR		OfdmRateLevel;

	UCHAR		RatePolicy;
	UCHAR		reserved_3[3];

	// For RSSI turning
	LONG		RSSI_high;
	LONG		RSSI_low;

} MTO_PARAMETERS, *PMTO_PARAMETERS;



// LA20040210_DTO kevin
//#define MTO_FUNC_INPUT              IN PWB32_ADAPTER Adapter
//#define MTO_FUNC_INPUT_DATA         Adapter
//#define MTO_DATA()                  (Adapter->mto_data)
//#define MTO_HAL()                   (&Adapter->hw_data)
//#define MTO_FUNC_INPUT              core_data_t *pcore_data
#define MTO_FUNC_INPUT              PWB32_ADAPTER	Adapter
#define MTO_FUNC_INPUT_DATA         Adapter
#define MTO_DATA()                  (Adapter->sMtoPara)
#define MTO_HAL()                   (&Adapter->sHwData)
#define MTO_SET_PREAMBLE_TYPE(x)    // 20040511 Turbo mark LM_PREAMBLE_TYPE(&pcore_data->lm_data) = (x)
//#define MTO_ENABLE					(Adapter->sLocalPara.MTOExecuting)	//[WKCHEN]
#define MTO_ENABLE					(Adapter->sLocalPara.TxRateMode == RATE_AUTO)
#define MTO_TXPOWER_FROM_EEPROM		(Adapter->sHwData.PowerIndexFromEEPROM)
#define LOCAL_ANTENNA_NO()			(Adapter->sLocalPara.bAntennaNo)
#define LOCAL_IS_CONNECTED()		(Adapter->sLocalPara.wConnectedSTAindex != 0)
#define LOCAL_IS_IBSS_MODE()		(Adapter->asBSSDescriptElement[Adapter->sLocalPara.wConnectedSTAindex].bBssType == IBSS_NET)
#define MTO_INITTXRATE_MODE			(Adapter->sHwData.SoftwareSet&0x2)	//bit 1
// 20040510 Turbo add
#define MTO_TMR_CNT()               MTO_DATA().TmrCnt
#define MTO_TOGGLE_STATE()          MTO_DATA().ToggleState
#define MTO_TX_RATE_REDUCTION_STATE() MTO_DATA().TxRateReductionState
#define MTO_BACKOFF_TMR()           MTO_DATA().BackoffTmr
#define MTO_LAST_RATE()             MTO_DATA().Last_Rate
#define MTO_CO_EFFICENT()           MTO_DATA().Co_efficent

//#define MTO_TH_FIXANT()           MTO_DATA().Th_Fixant
#define MTO_TH_CNT()                MTO_DATA().Th_Cnt
#define MTO_TH_SQ3()                MTO_DATA().Th_SQ3
#define MTO_TH_IDLE_SLOT()          MTO_DATA().Th_IdleSlot
#define MTO_TH_PR_INTERF()          MTO_DATA().Th_PrInterf

#define MTO_TMR_AGING()             MTO_DATA().Tmr_Aging
#define MTO_TMR_PERIODIC()          MTO_DATA().Tmr_Periodic

#define MTO_POWER_CHANGE_ENABLE()   MTO_DATA().PowerChangeEnable
//#define MTO_ANT_DIVERSITY_ENABLE()  MTO_DATA().AntDiversityEnable
#define MTO_ANT_DIVERSITY_ENABLE()  Adapter->sLocalPara.boAntennaDiversity
#define MTO_ANT_MAC()               MTO_DATA().Ant_mac
#define MTO_ANT_DIVERSITY()         MTO_DATA().Ant_div
#define MTO_CCA_MODE()              MTO_DATA().CCA_Mode
#define MTO_CCA_MODE_SETUP()        MTO_DATA().CCA_Mode_Setup
#define MTO_PREAMBLE_TYPE()         MTO_DATA().Preamble_Type
#define MTO_PREAMBLE_CHANGE_ENABLE()         MTO_DATA().PreambleChangeEnable

#define MTO_RATE_LEVEL()            MTO_DATA().DataRateLevel
#define MTO_FALLBACK_RATE_LEVEL()	MTO_DATA().FallbackRateLevel
#define MTO_OFDM_RATE_LEVEL()		MTO_DATA().OfdmRateLevel
#define MTO_RATE_CHANGE_ENABLE()    MTO_DATA().DataRateChangeEnable
#define MTO_FRAG_TH_LEVEL()         MTO_DATA().FragThresholdLevel
#define MTO_FRAG_CHANGE_ENABLE()    MTO_DATA().FragThresholdChangeEnable
#define MTO_RTS_THRESHOLD()         MTO_DATA().RTSThreshold
#define MTO_RTS_CHANGE_ENABLE()     MTO_DATA().RTSChangeEnable
#define MTO_RTS_THRESHOLD_SETUP()   MTO_DATA().RTSThreshold_Setup

#define MTO_AVG_IDLE_SLOT()         MTO_DATA().AvgIdleSlot
#define MTO_PR_INTERF()             MTO_DATA().Pr_Interf
#define MTO_AVG_GAP_BTWN_INTERF()   MTO_DATA().AvgGapBtwnInterf

#define MTO_ANT_SEL()               MTO_DATA().Ant_sel
#define MTO_CNT_ANT(x)              MTO_DATA().Cnt_Ant[(x)]
#define MTO_SQ_ANT(x)               MTO_DATA().SQ_Ant[(x)]
#define MTO_AGING_TIMEOUT()         MTO_DATA().aging_timeout


#define MTO_TXFLOWCOUNT()			MTO_DATA().TxFlowCount
//--------- DTO threshold parameters -------------
#define	MTOPARA_PERIODIC_CHECK_CYCLE()		MTO_DATA().DTO_PeriodicCheckCycle
#define	MTOPARA_RSSI_TH_FOR_ANTDIV()		MTO_DATA().DTO_RssiThForAntDiv
#define	MTOPARA_TXCOUNT_TH_FOR_CALC_RATE()	MTO_DATA().DTO_TxCountThForCalcNewRate
#define	MTOPARA_TXRATE_INC_TH()				MTO_DATA().DTO_TxRateIncTh
#define	MTOPARA_TXRATE_DEC_TH()				MTO_DATA().DTO_TxRateDecTh
#define MTOPARA_TXRATE_EQ_TH()				MTO_DATA().DTO_TxRateEqTh
#define	MTOPARA_TXRATE_BACKOFF()			MTO_DATA().DTO_TxRateBackOff
#define	MTOPARA_TXRETRYRATE_REDUCE()		MTO_DATA().DTO_TxRetryRateReduce
#define MTOPARA_TXPOWER_INDEX()				MTO_DATA().DTO_TxPowerIndex
//------------------------------------------------


// LA20040210_DTO kevin
//void MTO_Init(...);
//void MTO_PeriodicTimerExpired(...);
//void MTO_AgingTimerExpired(...);
//void MTO_TxFailed(...);
//void MTO_Interf(...);
//void MTO_Reset(...);

extern UCHAR   MTO_Data_Rate_Tbl[];
extern USHORT  MTO_Frag_Th_Tbl[];

#define MTO_DATA_RATE()             MTO_Data_Rate_Tbl[MTO_RATE_LEVEL()]
#define MTO_DATA_FALLBACK_RATE()	MTO_Data_Rate_Tbl[MTO_FALLBACK_RATE_LEVEL()]	//next level
#define MTO_FRAG_TH()               MTO_Frag_Th_Tbl[MTO_FRAG_TH_LEVEL()]

typedef struct{
	UCHAR tx_rate;
	UCHAR tx_retry_rate;
}TXRETRY_REC;

typedef struct _STATISTICS_INFO
{
	ULONG   Rate54M;
	ULONG   Rate48M;
	ULONG   Rate36M;
	ULONG   Rate24M;
	ULONG   Rate18M;
	ULONG   Rate12M;
	ULONG   Rate9M;
	ULONG   Rate6M;
	ULONG   Rate11MS;
	ULONG   Rate11ML;
	ULONG   Rate55MS;
	ULONG   Rate55ML;
	ULONG   Rate2MS;
	ULONG   Rate2ML;
	ULONG   Rate1M;
	ULONG   Rate54MOK;
	ULONG   Rate48MOK;
	ULONG   Rate36MOK;
	ULONG   Rate24MOK;
	ULONG   Rate18MOK;
	ULONG   Rate12MOK;
	ULONG   Rate9MOK;
	ULONG   Rate6MOK;
	ULONG   Rate11MSOK;
	ULONG   Rate11MLOK;
	ULONG   Rate55MSOK;
	ULONG   Rate55MLOK;
	ULONG   Rate2MSOK;
	ULONG   Rate2MLOK;
	ULONG   Rate1MOK;
	ULONG   SQ3;
	INT   RSSIAVG;
	INT   RSSIMAX;
	INT   TXRATE;
	INT   TxRetryRate;
	INT   BSS_PK_CNT;
	INT   NIDLESLOT;
	INT   SLOT_CNT;
	INT   INTERF_CNT;
	INT   GAP_CNT;
	INT   DS_EVM;
	INT   RcvBeaconNum;
	INT   RXRATE;
	INT   RxBytes;
	INT   TxBytes;
	INT   Antenna;
}STATISTICS_INFO, *PSTATISTICS_INFO;

#endif //__MTO_H__


